<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.


if (isset($_POST["submit_currency"])){
    $root_path = "../";
    include("../includes/utils.php");
    $source = $_POST["source"];
    $target = $_POST["target"];
    foreach ($_POST as $id => $value){
        if (str_startswith($id, "currency")){
            $value = $_POST[$id];
            $url = "http://www.exchangerate-api.com/$source/$target/$value?k=FOKr8-K3MRK-fwTkV";
            $result = file_get_contents($url);
            echo "<div id='${id}_result'>$result $target</div>";
        }
    }

} else {

    include_once("ext_lib/simple_html_dom.php");

    function execute_currency($site_content){
        $html = str_get_html($site_content);

        $values = array();
        foreach($html->find("a") as $element){
            if ($element->class == "ekkin"){
                if ($element->id == "currency_value"){

                    $pieces = explode(" ", str_replace("&nbsp;", "", strip_tags($element->innertext)));

                    $value = strip_tags($pieces[0]);
                    $source = strip_tags($pieces[1]);
                    //$value = $element->href;
                    $id = random_string(10);
                    $cc_content = generate_cc_value($value, $source, $value_label, $id);
                    $element->outertext = $cc_content;
                    $values["currency_${value}_$id"] = $value;
                }
            }
        }

        foreach($html->find("a") as $element){
            if ($element->class == "ekkin"){
               if ($element->id == "currency_chooser"){
                    $source = $element->target;
                    $cc_content = generate_cc_chooser($values, $source);
                    $label = $element->innertext();
                    $element->outertext = "$label $cc_content";
                }
            }
        }

        return $html->save();
    }


}

function generate_cc_value($value, $source, $value_label, $id){

    $html = "<div style='display:inline' id=\"currency_${value}_${id}\" currency=\"$source\" value=\"$value\">$value $source</div>";


    return $html;


}

function generate_cc_chooser($values, $source){
    $source = strtoupper($source);
    if ($source == "EUR") $selected_eur = "selected";
    if ($source == "USD") $selected_usd = "selected";
    if ($source == "MXN") $selected_mxn = "selected";

    $unique = random_string(5);

    $html = "<div style='display: inline'>
    <form method='POST' id='form_currency' action='plugins/currency.php' onsubmit='return AIM.submit(this, {onStart : startCallback, onComplete : completeCallback})'>

    <select onchange=\"${unique}ChangeCC()\" name=\"target\">;
        <option value=\"EUR\" $selected_eur>"._("Euros")."</option>
        <option value=\"USD\" $selected_usd>"._("US Dollars")."</option>
        <option value=\"MXN\" $selected_mxn>"._("Mexican Pesos")."</option>
    </select></div>
";

    $script_set = "";

    foreach($values as $id => $value){
        $html .= "<input type=\"hidden\" value=\"$value\" name=\"$id\"/>";
        $script_set .= "document.getElementById('$id').innerHTML = document.getElementById('${id}_result').innerHTML;\n";

    }
    $html .= "
    <input type=\"hidden\" name=\"submit_currency\"/>
    <input type=\"hidden\" value=\"$source\" name=\"source\"/>
    </form>";

    $script = "
    <script type='text/javascript' src='${relpath}ext_lib/webtoolkit.aim.js'></script>
    <script language=\"Javascript\">

    function ${unique}ChangeCC(){

        // submit
        document.getElementById('aap').innerHTML = '';

        AIM.submit(document.forms['form_currency'], {onStart : startCallback, onComplete : completeCallback});
        document.forms['form_currency'].submit();
    }

    function startCallback(){
        return true;
    }

    function completeCallback(response){
        document.getElementById('aap').innerHTML = response;
        $script_set
    }

    </script>
    <div id='aap' style='display:none'></div>
    ";

    return $script.$html;


}

?>
